/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Figures A13a, A13b, A13c and A13d
=========================================================================*/     

use "${data}/data_RD", clear
keep if icfes_per==20122 | icfes_per==20132 | icfes_per==20142
tab icfes_per
drop if icfes_stratum=="Sin Estrato"
drop if puntaje_global_s11_final==.

xtile decile122=puntaje_global_s11_final if icfes_per==20122, nq(10)
xtile decile132=puntaje_global_s11_final if icfes_per==20132, nq(10)
xtile decile142=puntaje_global_s11_final if icfes_per==20142, nq(10)
gen decile=decile142
replace decile=decile132 if icfes_per==20132
replace decile=decile122 if icfes_per==20122
drop decile1*

gegen aux=tag(codigo_ies icfes_per) if access_0==1
gegen nump=total(aux), by(codigo_ies)
tab nump,m
keep if nump==3 | nump==0
drop if codigo_ies!=. & nump==0

drop if icfes_per==20122
gen d_post=(icfes_per==20142)
replace codigo_programa=. if access_6==0
replace codigo_programa=0 if codigo_programa==.
rename codigo_programa codigo_prog
merge m:1 codigo_prog using "${Intermediate}/va_prog", keepusing(*_c_*) keep(1 3) nogen

replace decile=5 if decile<5
gen d0=.
gen d1=.
forvalues i=5/10{
	replace d0=`i' + 0.1 in `=`i'-4'
	replace d1=`i' - 0.1 in `=`i'-4'
	gen post_d`i'=(d_post==1 & decile==`i')
}

label define d0 5 "5 and below"
label values d0 d0

forvalues j=0/1{
	reghdfe graduation_exam_pro_7_c_va post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel)  ysc(r(-.025 .1)) ylab(0(.05).1) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA13a.pdf", replace

forvalues j=0/1{
	reghdfe score_saberpro_5_c_va post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.025 .1)) ylab(0(.05).1) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA13b.pdf", replace

forvalues j=0/1{
	reghdfe work_9_c_va post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.01 .05)) ylab(0(.01).05) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA13c.pdf", replace

forvalues j=0/1{
	reghdfe mw_9_c_va post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.025 .2)) ylab(0(0.05)0.2) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA13d.pdf", replace

